/*
 * Copyright 2016 Yan Zhenjie
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.com.egova.permission.szzt.lib;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import cn.com.egova.permission.R;


/**
 * Created by Yan Zhenjie on 2016/12/27.
 */
public class SettingDialog {

    private CustomDialog mDialog;
    private CustomDialog.OnClickListener mPositiveListener;
    private CustomDialog.OnClickListener mNegativeListener;
    private Context mContext;

    private SettingService mSettingService;
    private Runnable mPositiveAction;
    private Runnable mNegativeAction;

    SettingDialog(@NonNull Context context, @NonNull SettingService settingService) {
        mContext = context;
        mDialog = new CustomDialog(context);
        mPositiveListener = new CustomDialog.OnClickListener() {
            @Override
            public boolean onClick(CustomDialog dialog) {
                mClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                return false;
            }
        };
        mNegativeListener = new CustomDialog.OnClickListener() {
            @Override
            public boolean onClick(CustomDialog dialog) {
                mClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                return false;
            }
        };
        mDialog.setIsCancelable(false)
                .showTitle(R.string.permission_title_permission_failed)
                .setContent(R.string.permission_message_permission_failed)
                .setConfirmButton(context.getString(R.string.permission_setting),
                        mPositiveListener)
                .setCancelButton(context.getString(R.string.permission_cancel),
                        mNegativeListener);

        this.mSettingService = settingService;
    }

    @NonNull
    public SettingDialog setTitle(@NonNull String title) {
        mDialog.setTitle(title);
        return this;
    }

    @NonNull
    public SettingDialog setTitle(@StringRes int title) {
        mDialog.setTitle(title);
        return this;
    }

    @NonNull
    public SettingDialog setMessage(@NonNull String message) {
        mDialog.setContent(message);
        return this;
    }

    @NonNull
    public SettingDialog setMessage(@StringRes int message) {
        mDialog.setContent(message);
        return this;
    }

    public SettingDialog setPositiveAction(Runnable action) {
        this.mPositiveAction = action;
        return this;
    }

    public SettingDialog setNegativeAction(Runnable action) {
        this.mNegativeAction = action;
        return this;
    }

    @NonNull
    public SettingDialog setNegativeButton(@NonNull String text, @Nullable final DialogInterface.OnClickListener
            negativeListener) {
        mDialog.setCancelButton(text, new CustomDialog.OnClickListener() {
            @Override
            public boolean onClick(CustomDialog dialog) {
                if (negativeListener != null) {
                    negativeListener.onClick(dialog, Dialog.BUTTON_NEGATIVE);
                }
                return false;
            }
        });
        return this;
    }

    @NonNull
    public SettingDialog setNegativeButton(@StringRes int text, @Nullable DialogInterface.OnClickListener
            negativeListener) {
        return setNegativeButton(mContext.getString(text), negativeListener);
    }

    @NonNull
    public SettingDialog setPositiveButton(@NonNull String text, @Nullable final DialogInterface.OnClickListener
            mPositiveListener) {
        mDialog.setConfirmButton(text, new CustomDialog.OnClickListener() {
            @Override
            public boolean onClick(CustomDialog dialog) {
                if (mPositiveListener != null) {
                    mPositiveListener.onClick(dialog, Dialog.BUTTON_POSITIVE);
                }
                return false;
            }
        });
        return this;
    }

    @NonNull
    public SettingDialog setPositiveButton(@StringRes int text, @Nullable DialogInterface.OnClickListener
            mPositiveListener) {
        return setPositiveButton(mContext.getString(text), mPositiveListener);
    }

    @NonNull
    public SettingDialog setPositiveButton(@NonNull String text) {
        mDialog.setConfirmButton(text, mPositiveListener);
        return this;
    }

    @NonNull
    public SettingDialog setPositiveButton(@StringRes int text) {
        return setPositiveButton(mContext.getString(text));
    }

    public void show() {
        mDialog.show();
    }

    /**
     * The dialog's btn click listener.
     */
    private DialogInterface.OnClickListener mClickListener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case DialogInterface.BUTTON_NEGATIVE:
                    mSettingService.cancel();
                    if (mNegativeAction != null) {
                        mNegativeAction.run();
                    }
                    break;
                case DialogInterface.BUTTON_POSITIVE:
                    mSettingService.execute();
                    if (mPositiveAction != null) {
                        mPositiveAction.run();
                    }
                    break;
            }
        }
    };
}
